css: Add -gtk-dpi CSS property hack
authorBenjamin Otte <otte@redhat.com>
Thu, 5 Feb 2015 17:52:59 +0000 (18:52 +0100)
committerBenjamin Otte <otte@redhat.com>
Thu, 5 Feb 2015 17:57:29 +0000 (18:57 +0100)
This property is necessary to ensure widgets automatically update after
the text scale factor is changed desktop-wide.

And if I'm already doing a property like this, I can make it
overridable. So now you can override the dpi per-widget with CSS like
GtkSwitch {
  -gtk-dpi: 48;
}
if you want to debug things.

Long-term, we want to get rid of this property and insist on dpi being
96 everywhere and people can change the font size to get larger fonts.

gtk/gtkcssinitialvalue.c
gtk/gtkcssstylepropertyimpl.c
gtk/gtkcsstypesprivate.h
gtk/gtkwidget.c

index 5e2aa91da5592a8729026cf118c0ac281a50967e..0a16cb940c91ae15228cb5697c78bc0bb609de16 100644 (file)
@@ -23,6 +23,7 @@
 #include "gtkcssnumbervalueprivate.h"
 #include "gtkcssstringvalueprivate.h"
 #include "gtkcssstylepropertyprivate.h"
+#include "gtksettingsprivate.h"
 #include "gtkstyleproviderprivate.h"
 
 struct _GtkCssValue {
@@ -49,6 +50,18 @@ gtk_css_value_initial_compute (GtkCssValue             *value,
 
   switch (property_id)
     {
+    case GTK_CSS_PROPERTY_DPI:
+      settings = _gtk_style_provider_private_get_settings (provider);
+      if (settings)
+        {
+          GdkScreen *screen = _gtk_settings_get_screen (settings);
+          double resolution = gdk_screen_get_resolution (screen);
+
+          if (resolution > 0.0)
+            return _gtk_css_number_value_new (resolution, GTK_CSS_NUMBER);
+        }
+      break;
+
     case GTK_CSS_PROPERTY_FONT_FAMILY:
       settings = _gtk_style_provider_private_get_settings (provider);
       if (settings)
index 9f90184d9f7a151a00700ede68d347ae02440d1c..056c54ff82fd1ac0cd87d9f75c625ffcd8a0e593 100644 (file)
@@ -731,6 +731,13 @@ background_image_value_assign (GtkCssStyleProperty *property,
   return _gtk_css_array_value_new (css_image_value_assign (property, value));
 }
 
+static GtkCssValue *
+dpi_parse (GtkCssStyleProperty *property,
+          GtkCssParser        *parser)
+{
+  return _gtk_css_number_value_parse (parser, GTK_CSS_PARSE_NUMBER);
+}
+
 static GtkCssValue *
 font_size_parse (GtkCssStyleProperty *property,
                  GtkCssParser        *parser)
@@ -959,7 +966,7 @@ icon_theme_value_parse (GtkCssStyleProperty *property,
 void
 _gtk_css_style_property_init_properties (void)
 {
-  /* Initialize "color" and "font-size" first,
+  /* Initialize "color", "-gtk-dpi" and "font-size" first,
    * so that when computing values later they are
    * done first. That way, 'currentColor' and font
    * sizes in em can be looked up properly */
@@ -972,6 +979,15 @@ _gtk_css_style_property_init_properties (void)
                                           color_query,
                                           color_assign,
                                           _gtk_css_color_value_new_rgba (1, 1, 1, 1));
+  gtk_css_style_property_register        ("-gtk-dpi",
+                                          GTK_CSS_PROPERTY_DPI,
+                                          G_TYPE_NONE,
+                                          GTK_STYLE_PROPERTY_INHERIT | GTK_STYLE_PROPERTY_ANIMATED,
+                                          GTK_CSS_AFFECTS_FONT | GTK_CSS_AFFECTS_TEXT | GTK_CSS_AFFECTS_SIZE,
+                                          dpi_parse,
+                                          NULL,
+                                          NULL,
+                                          _gtk_css_number_value_new (96.0, GTK_CSS_NUMBER));
   gtk_css_style_property_register        ("font-size",
                                           GTK_CSS_PROPERTY_FONT_SIZE,
                                           G_TYPE_DOUBLE,
index 1f96f7d7bb4002641a757a9fb06eb18ecd252052..2308453eabb575080062ac9314bad8f8608c0641 100644 (file)
@@ -110,6 +110,7 @@ typedef enum {
 
 enum { /*< skip >*/
   GTK_CSS_PROPERTY_COLOR,
+  GTK_CSS_PROPERTY_DPI,
   GTK_CSS_PROPERTY_FONT_SIZE,
   GTK_CSS_PROPERTY_ICON_THEME,
   GTK_CSS_PROPERTY_BACKGROUND_COLOR,
index f6c83bb7eacc669c0243852bc07e12458d8cce55..2a9769b1e75b4ec842335a9ebd43819b537ccca3 100644 (file)
@@ -10286,6 +10286,12 @@ update_pango_context (GtkWidget    *widget,
                              PANGO_DIRECTION_LTR : PANGO_DIRECTION_RTL);
 
   pango_font_description_free (font_desc);
+
+  pango_cairo_context_set_resolution (context,
+                                      _gtk_css_number_value_get (
+                                          _gtk_style_context_peek_property (style_context,
+                                                                            GTK_CSS_PROPERTY_DPI),
+                                          100));
 }
 
 static void
@@ -10302,8 +10308,6 @@ gtk_widget_update_pango_context (GtkWidget *widget)
       screen = gtk_widget_get_screen_unchecked (widget);
       if (screen)
        {
-         pango_cairo_context_set_resolution (context,
-                                             gdk_screen_get_resolution (screen));
          pango_cairo_context_set_font_options (context,
                                                gdk_screen_get_font_options (screen));
        }